﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public partial class EditLegendForm : Form
    {
        public EditLegendForm()
        {
            InitializeComponent();
        }

        private void EditLegendForm_Load(object sender, EventArgs e)
        {

        }

        private void SetLegendListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {

        }

        //添加新的图例
        private void AddSymbolButton_Click(object sender, EventArgs e)
        {
            LegendForm legendForm = new LegendForm();
            legendForm.ShowDialog();
            RefreshData();
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
        }

        public void RefreshData()
        {
            int row = dataGridView1.Rows.Count;
            for (int i = 0; i < row;i ++)
            {
                dataGridView1.Rows.RemoveAt(0);
            }
            for (int i = 0; i < Controller.MapLegends.Count; i++)
            {
                dataGridView1.Rows.Add(new DataGridViewRow());
                dataGridView1.Rows[i].Cells[0].Value = Controller.MapLegends[i].GetID();
                dataGridView1.Rows[i].Cells[3].Value = Controller.MapLegends[i].Title;
                dataGridView1.Rows[i].Cells[4].Value = Controller.MapLegends[i].Subtitle;
                if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CPointLegend)
                {
                    dataGridView1.Rows[i].Cells[1].Value = "点";
                    Image symbol;
                    switch (((CPointLegend)Controller.MapLegends[i]).Point_Style)
                    {
                        case CPointLegend.PointStyle.circle:
                            symbol = imageList1.Images[0];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CPointLegend.PointStyle.filled_circle:
                            symbol = imageList1.Images[1];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CPointLegend.PointStyle.rectangle:
                            symbol = imageList1.Images[2];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CPointLegend.PointStyle.triangle:
                            symbol = imageList1.Images[3];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CPointLegend.PointStyle.filled_triangle:
                            symbol = imageList1.Images[4];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CPointLegend.PointStyle.filled_rectangle:
                            symbol = imageList1.Images[5];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                    }
                }
                else if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CLineLegend)
                {
                    dataGridView1.Rows[i].Cells[1].Value = "线";
                    Image symbol;
                    switch (((CLineLegend)Controller.MapLegends[i]).Line_Style)
                    {
                        case CLineLegend.LineStyle.solidline:
                            symbol = imageList1.Images[9];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CLineLegend.LineStyle.dashline:
                            symbol = imageList1.Images[7];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                    }
                }
                else if (Controller.MapLegends[i].Legend_Type == CLegend.LegendType.CPolygonLegend)
                {
                    dataGridView1.Rows[i].Cells[1].Value = "面";
                    Image symbol;
                    switch (((CPolygonLegend)Controller.MapLegends[i]).Fill_Style)
                    {
                        case CPolygonLegend.FillStyle.solid:
                            symbol = imageList1.Images[8];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                        case CPolygonLegend.FillStyle.transparent:
                            symbol = imageList1.Images[6];
                            dataGridView1.Rows[i].Cells[2].Value = symbol;
                            break;
                    }
                }
                else
                {
                    dataGridView1.Rows[i].Cells[1].Value = "文本";
                    dataGridView1.Rows[i].Cells[2].Value = imageList1.Images[10];
                }
            }
        }

        private void EditLegendForm_Activated(object sender, EventArgs e)
        { 
        }

        //确定选择某一套图例
        private void OKbutton_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
            int curIndex = FromIdToIndex(ID);
            string TYPE = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            if ((TYPE != "点" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPointLayer) || (TYPE == "点" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CPointLayer) || (TYPE != "线" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CLineLayer) || (TYPE == "线" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CLineLayer) || (TYPE != "面" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPolygonLayer) || (TYPE == "面" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CPolygonLayer) || (TYPE != "文本" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CTextLayer) || (TYPE == "文本" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CTextLayer))
            {
                MessageBox.Show("警告：图层类型和图例类型不一致！无法选择。");
            }
            else
            {
                Controller.CurrentLegendIndex = curIndex;
                Controller.current_Operation = Controller.OperationStyles.Editing;
                this.Close();
            }
        }

        //编辑图例
        private void EditSymbolButton_Click(object sender, EventArgs e)
        {
            int SelectedRowIndex, ID;
            string TYPE = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            if ((TYPE != "点" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPointLayer) || (TYPE == "点" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CPointLayer) || (TYPE != "线" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CLineLayer) || (TYPE == "线" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CLineLayer) || (TYPE != "面" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CPolygonLayer) || (TYPE == "面" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CPolygonLayer) || (TYPE != "文本" && Controller.CurrentLayer.Layer_Type == CLayer.LayerType.CTextLayer) || (TYPE == "文本" && Controller.CurrentLayer.Layer_Type != CLayer.LayerType.CTextLayer))
            {
                MessageBox.Show("警告：您选择的图层并非当前编辑图层！请选择当前编辑图层以后再进行此操作。");
            }
            else
            {
                SelectedRowIndex = dataGridView1.CurrentRow.Index;
                ID = Convert.ToInt32(dataGridView1.Rows[SelectedRowIndex].Cells[0].Value);
                Controller.WaitForChangeLegendIndex = FromIdToIndex(ID);
                LegendForm legendForm = new LegendForm();
                int Index = FromIdToIndex(ID);
                legendForm.status = "修改图例";
                legendForm.newRefresh(Index);
                legendForm.ShowDialog();
                this.RefreshData();
                if (legendForm.titleChanged == true && legendForm.isChanged == false)
                {
                }
                else//只有当用户改变了非标题图例信息，这时候就重绘
                {
                    PictureBox NewLegendTemp = new PictureBox();
                    NewLegendTemp = Controller.FindLayer(Controller.CurrentLayer.GetID());
                    switch (Controller.CurrentLayer.Layer_Type)
                    {
                        case CLayer.LayerType.CPointLayer:
                            NewLegendTemp.Image = ((CPointLayer)Controller.CurrentLayer).drawpoint();
                            break;
                        case CLayer.LayerType.CLineLayer:
                            NewLegendTemp.Image = ((CLineLayer)Controller.CurrentLayer).drawlinelayer();
                            break;
                        case CLayer.LayerType.CPolygonLayer:
                            NewLegendTemp.Image = ((CPolygonLayer)Controller.CurrentLayer).drawpolygon();
                            break;
                        case CLayer.LayerType.CTextLayer:
                            NewLegendTemp.Image = ((CTextLayer)Controller.CurrentLayer).drawtext();
                            break;
                    }
                }
            }
        }

        //找到要编辑的图例在Maplegends里的Index
        private int FromIdToIndex(int id)
        {
            for (int i = 0; i < Controller.MapLegends.Count; i++)
            {
                if (Controller.MapLegends[i].GetID() == id)
                    return i;
                else continue;
            }
            return 0;//return0不对吧，但先这样
        }

        //显示在图例窗口
        private void DeleteSymbolButton_Click(object sender, EventArgs e)
        {
            MainForm.f1.DisplayLegend();
        }
    }
}
